from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

def train_and_predict(X, y, test_size=0.2, random_state=None):
    '''
    sklearn训练线性回归模型并预测
    
    Args:
        X: 特征矩阵
        y: 目标量
        test_size: 测试集比例
        random_state: 随机种子
    Returns:
        model: 训练好的模型
        X_test: 测试集特征
        y_test: 测试集标签
        predictions: 预测结果
    '''
    # 划分训练集和测试集
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=test_size, random_state=random_state
    )
    
    # 创建并训练模型
    model = LinearRegression()
    model.fit(X_train, y_train)
    
    # 预测
    predictions = model.predict(X_test)
    
    return model, X_test, y_test, predictions